/**
 * WinMgr.ts
 * Author：qiuchun(362460803@qq.com)
 * Time:Thu Jun 15 2023 16:28:46 GMT+0800 (中国标准时间)
 * Desc:窗口管理器
 * 
 */

import { rejects } from "assert";
import { AssetManager, Prefab, Scene, assetManager, director, find, instantiate, log, error, warn, resources, UITransform, Vec3, tween, easing, TweenAction, Sprite, Color, Node, UIOpacity } from "cc";
import { resolve } from "path";


export class WinMgr {

    showWin(path: string, action?: string, duration?: number) {
        this.printCache();
        resources.load(path, Prefab,
            (finished: number, total: number, item: AssetManager.RequestItem) => {
                log("finished:%d", finished);
                log("total:%d", total);
                log(item);
            },
            (err: Error, data: Prefab) => {
                if (err) {
                    error("game error:%s", err.message);
                    return;
                }
                let parent = find("Canvas");
                if (parent == null) {
                    error("game error: \'Canvas/bg\' not exist!");
                }

                let winNode = instantiate(data);
                parent.addChild(winNode);
                winNode.setPosition(Vec3.ZERO);

                duration = duration || 0.5;
                if (action == "scale") {
                    this.tweenScale(winNode, duration);
                }
                else if (action == "fade") {
                    let uiOpacity = winNode.getComponent(UIOpacity);
                    if (uiOpacity) {
                        this.tweenFade(uiOpacity, duration);
                    }
                    else {
                        this.tweenScale(winNode, duration);
                    }
                }
                this.printCache();

            })
    }

    load(path: string) {
        return new Promise((resolve, rejects) => {
            resources.load(path, Prefab, (err: Error, data: Prefab) => {
                if (err) {
                    rejects(err.message);
                    return;
                }

                resolve(data);
            })
        })
    }

    async showWinAsync(path: string, action?: string, duration?: number) {

        await this.load(path).then((data: Prefab) => {
            if (data) {
                let parent = find("Canvas");
                if (parent == null) {
                    warn("game error: \'Canvas/bg\' not exist!");
                    return;
                }

                let winNode = instantiate(data);
                parent.addChild(winNode);
                winNode.setPosition(Vec3.ZERO);
            }
        })

        log("执行完成。");
    }

    //缩放出场
    tweenScale(target: Node, duration: number) {
        tween(target).stop();
        target.setScale(new Vec3(0.5, 0.5, 0.5));
        tween(target).to(duration, { scale: new Vec3(1.0, 1.0, 1.0) }, { easing: "backOut" })
            .start();
    }

    //渐变出场,要配合UIOpacity组件
    tweenFade(uiOpacity: UIOpacity, duration: number) {
        class Target {
            opacity: number;
        };


        uiOpacity.opacity = 0;
        let newTarget = new Target();
        newTarget.opacity = 0;
        tween(newTarget).to(duration, { opacity: 255 }, {
            easing: "fade",
            onUpdate(target: Target, ratio) {
                uiOpacity.opacity = target.opacity;
            },
        })
            .start();
    }

    printCache() {

        let count = 0;
        if (assetManager.cacheManager) {
            count = assetManager.cacheManager.cachedFiles.count;
        }

        log("缓存数量：%d", count);
    }



}

export let winMgr = new WinMgr();

